package simulation;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import streaming.StreamingConfig;

public class Util {

	// ------------------------
	// rpad
	// ------------------------
	public static String rpad(String s, int n) {
		if (StreamingConfig.REMOVE_ALL_SPACES_IN_LOG_STRINGS == true) {
			return s;
		}
		return String.format("%1$-" + n + "s", s);  
	}
	public static String rpad(int s, int n) {
		if (StreamingConfig.REMOVE_ALL_SPACES_IN_LOG_STRINGS == true) {
			return ""+s;
		}
		return String.format("%1$-" + n + "s", ""+s);  
	}
	public static String rpad(double s, int n) {
		if (StreamingConfig.REMOVE_ALL_SPACES_IN_LOG_STRINGS == true) {
			return ""+s;
		}
		return String.format("%1$-" + n + "s", ""+s);  
	}
	
	// ------------------------
	// lpad
	// ------------------------
	public static String lpad(String s, int n) {
		if (StreamingConfig.REMOVE_ALL_SPACES_IN_LOG_STRINGS == true) {
			return s;
		}
	    return String.format("%1$" + n + "s", s);  
	}
	public static String lpad(int s, int n) {
		if (StreamingConfig.REMOVE_ALL_SPACES_IN_LOG_STRINGS == true) {
			return ""+s;
		}
	    return String.format("%1$" + n + "s", ""+s);  
	}
	public static String lpad(double s, int n) {
		if (StreamingConfig.REMOVE_ALL_SPACES_IN_LOG_STRINGS == true) {
			return ""+s;
		}
	    return String.format("%1$" + n + "s", ""+s);  
	}

	// ------------------------
	// trunc
	// ------------------------
	public static double trunc(double x, int precision)
	{
		double power = Math.pow(10,precision);
		
	    long y=(long)(x*power);
	    return (double)y/power;
	}
	
	public static long trunc(double x)
	{		
	    long y=(long)x;
	    return y;
	}
	
	// ------------------------
	// to_char
	// ------------------------

	// DOUBLE
	public static String to_char(double x)
	{
	    long y=(long)(x);
	    return ""+(double)y;
	}

	// DOUBLE (with precision)
	public static String to_char_old(double x, int precision)
	{
		double power = Math.pow(10,precision);
		
	    long y=(long)(x*power);
	    return ""+(double)y/power;
	}
	
	// DOUBLE (with precision)
	public static String to_char(double x, int precision)
	{
		String strPower = "";
		String strPrecision = "";
		String strResultTemp = "";
		String strResult = "";
		
		if (precision == 0) { strPrecision = "0"; }
		else {
			strPower = ""+(int)Math.pow(10,precision);
			strPrecision = strPower.substring(1,strPower.length());
			strPrecision = "0." + strPrecision;
		}
		DecimalFormat fmt = new DecimalFormat(strPrecision);
		strResultTemp = fmt.format(x);  
		strResult = strResultTemp.replace("," , ".");
		return strResult;
	}
	


	// DATE
	public static String to_char(Date dateTime)
	{
		String strDateTime = "";
		
		DateFormat formatter;
		formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		strDateTime = formatter.format(dateTime);
		
		return strDateTime;
	}
	
	// DATE
	public static String to_char2(Date dateTime)
	{
		String strDateTime = "";
		
		DateFormat formatter;
		formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		strDateTime = formatter.format(dateTime);
		
		return "["+strDateTime+"]";
	}
	
	// DATE (with fmt)
	public static String to_char(Date dateTime, String fmt)
	{
		String strDateTime = "";
		
		DateFormat formatter;
		formatter = new SimpleDateFormat( fmt );
		strDateTime = formatter.format(dateTime);
		
		return strDateTime;
	}

	// ------------------------
	// sysdate
	// ------------------------
	public static Date sysdate()
	{
		Date dataHora = new Date();
	    return dataHora;
	}

	// SYSDATE2 (string) com colchetes
	public static String sysdate2()
	{
		Date dateTime = Util.sysdate();
	    return "["+ to_char(dateTime) +"]";
	}

}


